package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cxyd.common.constant.Constant;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.wms.dao.PostSapDao;
import com.cxyd.modules.wms.dto.PostSapDTO;
import com.cxyd.modules.wms.entity.PostSapEntity;
import com.cxyd.modules.wms.service.PostSapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PostSapServiceImpl extends CrudServiceImpl<PostSapDao, PostSapEntity, PostSapDTO> implements PostSapService {
    @Override
    public QueryWrapper<PostSapEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public void updateByKey(Map<String, Object> params) {
        String zkey = (String) params.get("zkey");
        String msgType = (String) params.get("msgType");
        String msgContent = (String) params.get("msgContent");
        final UpdateWrapper<PostSapEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ZKEY", zkey);
        updateWrapper.set("post_st", ("S".equals(msgType)) ? 2 : 9);
        updateWrapper.set("msg_type", msgType);
        updateWrapper.set("msg_content", msgContent);
        updateWrapper.set("updater", Constant.ClientCode.SAP.getId());
        updateWrapper.set("update_date", new Date());
        baseDao.update(null, updateWrapper);
    }

    @Override
    public List<PostSapEntity> getRecords() {
        final List<PostSapEntity> entity
                = baseDao.selectList(new QueryWrapper<PostSapEntity>()
                .eq("post_st",0)
                .orderByAsc("post_id"));
        return entity;

    }

    @Override
    public PostSapDTO getOneRecord() {
        final PostSapEntity entity = baseDao.getOneRecord();
        return ConvertUtils.sourceToTarget(entity, PostSapDTO.class);
    }
}
